package com.tgpms.web.earlierStage.service.impl;

import java.util.Date;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.tgpms.common.Result;
import com.tgpms.exception.ExceptionExplain;
import com.tgpms.util.Common;
import com.tgpms.web.earlierStage.dao.CoreQuotaMapper;
import com.tgpms.web.earlierStage.entity.CoreQuota;
import com.tgpms.web.earlierStage.service.EarlierCoreQuotaService;

@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
public class EarlierCoreQuotaServiceImpl implements EarlierCoreQuotaService {
	
	@Autowired
	private CoreQuotaMapper coreQuotaMapper;

	@Override
	public Result addCoreQuota(CoreQuota coreQuota, Result result) {
		//查询唯一键是否重复
//		if(coreQuotaMapper.selByUniqueKey(coreQuota.getTaskId(),coreQuota.getCoreId())!=null) {
//			result.setSuccess(false);
//			result.setMsg(ExceptionExplain.ERROR_BY_INSERT_REPETITION.getExplain());
//			return result;
//		}
		//主键形式 yyyyMMddHHmmss+UUID
		coreQuota.setId(Common.fromDateFormats()+UUID.randomUUID().toString().replaceAll("-", "").toUpperCase());
		coreQuota.setCoreId(coreQuota.getId());
		coreQuota.setRowstamp(coreQuota.getId());
		coreQuota.setInsertDate(new Date());
		int i = coreQuotaMapper.insertSelective(coreQuota);
		if(i==1) {
			result.setMsg(ExceptionExplain.SUCCESS_BY_INSERT.getExplain());
			result.setData(coreQuota.getId());
		}else {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_INSERT.getExplain());
		}
		return result;
	}

	@Override
	public Result updateCoreQuota(CoreQuota coreQuota, Result result) {
		coreQuota.setModifyDate(new Date());
		int i = coreQuotaMapper.updateByPrimaryKeySelective(coreQuota);
		if(i==1) {
			result.setMsg(ExceptionExplain.SUCCESS_BY_UPDATE.getExplain());
		}else {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_UPDATE.getExplain());
		}
		return result;
	}

	@Override
	public Result deleteCoreQuota(String id, Result result) {
		int i = coreQuotaMapper.deleteByPrimaryKey(id);
		if(i==1) {
			result.setMsg(ExceptionExplain.SUCCESS_BY_DELETE.getExplain());
		}else {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_DELETE.getExplain());
		}
		return result;
	}

}
